package cn.mysdp;

import cn.mysdp.utils.ProcessUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.h2.tools.*;

import java.io.*;
import java.util.Map;

import static java.lang.System.exit;
import org.postgresql.Driver;

/**
 * @ClassName:
 * @Description:
 * @Author: SDP
 * @Date: 2020-10-30
 * Table:
 * Comment:
 */
@SpringBootApplication
@EnableCaching
@EnableTransactionManagement
@EnableAutoConfiguration
@ComponentScan({"cn.mysdp"})
@MapperScan("cn.mysdp.biz.repository")
public class Application {

    public static void main(String[] args) throws Exception {

        Class clz = Driver.class;
        JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.SkipTransientField, false);

        String fileEncoding = System.getProperty("file.encoding");
        if (!"utf-8".equals(fileEncoding.toLowerCase())) {
            System.err.println("Please set parameter first: -Dfile.encoding=utf-8");
            System.setProperty("file.encoding", "UTF-8");
//            exit(1);
        }

        String argJson = JSON.toJSONString(args);
        if (argJson.indexOf("-webAdminPassword") >= 0) {
            org.h2.tools.Server.main(args);
            return;
        }
        String home = System.getProperty("user.home");
        File f = new File(home + "/sdp.mv.db");
        if (!f.exists()) {
            System.err.println("Create default database...");
            Resource resource = new ClassPathResource("sdp.mv.db");
            InputStream is = resource.getInputStream();
            byte[] buf = new byte[1024];
            OutputStream os = new FileOutputStream(f);
            while(true) {
                int len = is.read(buf);
                if (len <= 0) {
                    break;
                }
                os.write(buf, 0, len);
            }
            is.close();
            os.close();
            System.err.println("Default database created.");
        }


        SpringApplication.run(Application.class, args);
    }

}